import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:hive/hive.dart';
import 'package:sleep_manager/data/family_info.dart';
import 'package:sleep_manager/ui/widget/custom_app_bar.dart';
import 'package:sleep_manager/utils/toast_utils.dart';

/// 添加家人
class AddFamilyPage extends StatefulWidget {

  final String title;

  final void Function()? onRefresh;

  const AddFamilyPage({super.key, required this.title, this.onRefresh});

  @override
  State<StatefulWidget> createState() {
    return _AddFamilyPageState();
  }

}

class _AddFamilyPageState extends State<AddFamilyPage> {

  final List<String> genderData = ['男','女'];

  final _nameController = TextEditingController();
  final _genderController = TextEditingController();
  final _ageController = TextEditingController();
  final _weightController = TextEditingController();
  final _idTypeController = TextEditingController();
  final _idNumberController = TextEditingController();

  // 顶部的信息编辑
  Widget _personInformation() {
    return Stack(
      alignment: Alignment.center,
      children: [
        Container(
          margin: const EdgeInsets.fromLTRB(16, 75, 16, 0),
          padding: const EdgeInsets.fromLTRB(16, 60, 16, 100),
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.circular(12),
            boxShadow: [
              BoxShadow(
                  color: Colors.black.withOpacity(0.1),
                  blurRadius: 8,
                  spreadRadius: 2,
                  offset: const Offset(4, 4))
            ]
          ),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              Row(
                children: [
                  Expanded(
                    flex: 1,
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        const Text(
                          '姓名',
                          style: TextStyle(
                            fontSize: 15,
                          ),
                        ),
                        const SizedBox(height: 10),
                        TextField(
                          maxLines: 1,
                          maxLength: 6,
                          controller: _nameController,
                          decoration: buildInputDecoration(),
                          style: const TextStyle(fontSize: 15, color: Colors.black),
                        )
                      ],
                    ),
                  ),
                  const SizedBox(width: 56),
                  Expanded(
                    flex: 1,
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        const Text(
                          '性别',
                          style: TextStyle(
                            fontSize: 15,
                          ),
                        ),
                        const SizedBox(height: 10),
                        TextField(
                          maxLines: 1,
                          maxLength: 1,
                          controller: _genderController,
                          decoration: buildInputDecoration(),
                          style: const TextStyle(fontSize: 15, color: Colors.black),
                        )
                      ],
                    ),
                  )
                ],
              ),
              const SizedBox(height: 12),
              Row(
                children: [
                  Expanded(
                    flex: 1,
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        const Text(
                          '年龄',
                          style: TextStyle(
                            fontSize: 15,
                          ),
                        ),
                        const SizedBox(height: 10),
                        TextField(
                          maxLines: 1,
                          maxLength: 3,
                          keyboardType: TextInputType.number, // 数字类型
                          controller: _ageController,
                          decoration: buildInputDecoration(),
                          style: const TextStyle(fontSize: 15, color: Colors.black),
                        )
                      ],
                    ),
                  ),
                  const SizedBox(width: 56),
                  Expanded(
                    flex: 1,
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        const Text(
                          '体重',
                          style: TextStyle(
                            fontSize: 15,
                          ),
                        ),
                        const SizedBox(height: 10),
                        TextField(
                          maxLines: 1,
                          maxLength: 3,
                          keyboardType: TextInputType.number,
                          controller: _weightController,
                          decoration: buildInputDecoration(),
                          style: const TextStyle(
                              fontSize: 15, color: Colors.black),
                        )
                      ],
                    ),
                  )
                ],
              ),
              const SizedBox(height: 12),
              Row(
                children: [
                  Expanded(
                    flex: 1,
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        const Text(
                          '证件类型',
                          style: TextStyle(
                            fontSize: 15,
                          ),
                        ),
                        const SizedBox(height: 10),
                        TextField(
                          maxLines: 1,
                          maxLength: 9,
                          controller: _idTypeController,
                          decoration: buildInputDecoration(),
                          style: const TextStyle(fontSize: 15, color: Colors.black),
                        )
                      ],
                    ),
                  )
                ],
              ),
              const SizedBox(height: 12),
              Row(
                children: [
                  Expanded(
                    flex: 1,
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        const Text(
                          '证件号码',
                          style: TextStyle(
                            fontSize: 15,
                          ),
                        ),
                        const SizedBox(height: 10),
                        TextField(
                          maxLines: 1,
                          maxLength: 18,
                          keyboardType: TextInputType.number,
                          controller: _idNumberController,
                          decoration: buildInputDecoration(),
                          style: const TextStyle(fontSize: 15, color: Colors.black),
                        )
                      ],
                    ),
                  )
                ],
              )
            ],
          ),
        ),
        Positioned(
          top: 25,
          child: SvgPicture.asset('assets/images/profile_picture_man.svg', width: 100, height: 100),
        )
      ],
    );
  }

  // 输入框
  InputDecoration buildInputDecoration() {
    return InputDecoration(
        counterText: "", // 不显示字符计数器
        isDense: true,  // 输入框的紧凑程度
        enabledBorder: OutlineInputBorder( // 输入框未获取焦点的边框样式
            borderRadius: BorderRadius.circular(8),
            borderSide: const BorderSide(width: 1, color: Color(0xFF707070))
        ),
        focusedBorder: OutlineInputBorder( // 输入框获取焦点的边框样式
            borderRadius: BorderRadius.circular(8),
            borderSide: const BorderSide(width: 1, color: Color(0xFF707070))
        ),
        contentPadding: const EdgeInsets.symmetric(vertical: 9, horizontal: 14)
    );
  }

  // 底部的确认按钮
  Widget _confirmButton() {
    return GestureDetector(
      onTap: _handleConfirm,
      child: Container(
        alignment: Alignment.center,
        margin: const EdgeInsets.fromLTRB(32, 40, 32, 0),
        padding: const EdgeInsets.symmetric(vertical: 12),
        decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(24),
            gradient: const LinearGradient(
                colors: [Color(0xFF80BFFF), Color(0xFF397AFB)],
                begin: Alignment.centerLeft,
                end: Alignment.centerRight)),
        child: const Text(
          '确认添加',
          style: TextStyle(
              fontSize: 18, color: Colors.white, fontWeight: FontWeight.bold),
        ),
      ),
    );
  }

  // 点击确认按钮
  void _handleConfirm() {
    if (_nameController.text.isEmpty) {
      showToast("姓名不能为空");
      return;
    }

    if (_genderController.text.isEmpty) {
      showToast("性别不能为空");
      return;
    }

    if (_ageController.text.isEmpty) {
      showToast("年龄不能为空");
      return;
    }

    if (_weightController.text.isEmpty) {
      showToast("体重不能为空");
      return;
    }

    if (_idTypeController.text.isEmpty) {
      showToast("证件类型不能为空");
      return;
    }

    if (_idNumberController.text.isEmpty) {
      showToast("证件号码不能为空");
      return;
    }

    final familyInfo = FamilyInfo(
        userName: _nameController.text,
        gender: _genderController.text,
        age: _ageController.text,
        weight: _weightController.text,
        idType: _idTypeController.text,
        idCard: _idNumberController.text
    );

    // 存储到 Hive
    Hive.box<FamilyInfo>('familyInfoBox').add(familyInfo);

    // 返回上一页并刷新
    Navigator.of(context).pop();
    widget.onRefresh?.call();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: CustomAppBar(
        title: widget.title,
        showBackButton: true,
      ),
      body: Container(
        color: const Color(0xFFF7F7F7),
        child: SingleChildScrollView(
          child: Column(
            mainAxisSize: MainAxisSize.max,
            children: [
              _personInformation(),
              _confirmButton()
            ],
          )
        )
      ),
    );
  }
}
